﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Knlght.Common
{
    /// <summary>
    /// 转换辅助类
    /// </summary>
    public static class ConvertHelper
    {
        public static int? ConvertInt32(string str)
        {
            int value = 0;
            bool result = int.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static uint? ConvertUInt32(string str)
        {
            uint value = 0;
            bool result = uint.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static short? ConvertInt16(string str)
        {
            short value = 0;
            bool result = short.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static ushort? ConvertUInt16(string str)
        {
            ushort value = 0;
            bool result = ushort.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static long? ConvertInt64(string str)
        {
            long value = 0;
            bool result = long.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static ulong? ConvertUInt64(string str)
        {
            ulong value = 0;
            bool result = ulong.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static byte? ConvertByte(string str)
        {
            byte value = 0;
            bool result = byte.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static sbyte? ConvertSByte(string str)
        {
            sbyte value = 0;
            bool result = sbyte.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }

        public static float? ConvertFloat(string str)
        {
            float value = 0;
            bool result = float.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static double? ConvertDouble(string str)
        {
            double value = 0;
            bool result = double.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static decimal? ConvertDecimal(string str)
        {
            decimal value = 0;
            bool result = decimal.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static bool? ConvertBoolean(string str)
        {
            bool value = false;
            bool result = bool.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static DateTime? ConvertDateTime(string str)
        {
            DateTime value = DateTime.Now;
            bool result = DateTime.TryParse(str, out value);
            if (result)
            {
                return value;
            }
            else
            {
                return null;
            }
        }
        public static object ConvertToNullable<T>(string str) where T : struct
        {
            var type = typeof(T);

            if (type == typeof(Int16))
            {
                return ConvertInt16(str);
            }
            else if (type == typeof(Int32))
            {
                return ConvertInt32(str);
            }
            else if (type == typeof(Int64))
            {
                return ConvertInt64(str);
            }
            else if (type == typeof(UInt16))
            {
                return ConvertUInt16(str);
            }
            else if (type == typeof(UInt32))
            {
                return ConvertUInt32(str);
            }
            else if (type == typeof(UInt64))
            {
                return ConvertUInt64(str);
            }
            else if (type == typeof(byte))
            {
                return ConvertByte(str);
            }
            else if (type == typeof(sbyte))
            {
                return ConvertSByte(str);
            }
            else if (type == typeof(float))
            {
                return ConvertFloat(str);
            }
            else if (type == typeof(double))
            {
                return ConvertDouble(str);
            }
            else if (type == typeof(decimal))
            {
                return ConvertDecimal(str);
            }
            else if (type == typeof(bool))
            {
                return ConvertBoolean(str);
            }
            else if (type == typeof(DateTime))
            {
                return ConvertDateTime(str);
            }
            return null;
        }


        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this string value)
        {
            return string.IsNullOrWhiteSpace(value);
        }
        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this Guid? value)
        {
            if (value == null)
                return true;
            return IsEmpty(value.Value);
        }
        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this Guid value)
        {
            if (value == Guid.Empty)
                return true;
            return false;
        }
        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this object value)
        {
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
